// run: $exec < input
#include <iostream>
#include <algorithm>
#include <vector>

struct data
{
	int count;
	std::string name;
};

bool operator<(data const & a, data const & b)
{
	return a.count > b.count;
}

int n;
std::vector<data> da;

int main()
{
	std::ios::sync_with_stdio(false);
	int T; std::cin >> T;
	while (T--) {
		std::cin >> n;
		da.resize(n);
		for (int i = 0; i < n; i++)
			std::cin >> da[i].name >> da[i].count;
		std::sort(da.begin(), da.end());
		std::cout << da[0].name;
		for (int i = 1; i < n; i++)
			std::cout << " " << da[i].name;
		std::cout << "\n";
	}
}

